import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description：
 * User：user
 * DATE:2021-10-30
 * Time:14:58
 */
public class TestDemo {

    public static void main(String[] args) {
        String str = "hello";
        byte[] bytes = str.getBytes();
        byte[] bytes2 = str.getBytes("utf-8");
        byte[] bytes3 = str.getBytes("GBK");
        System.out.println(Arrays.toString(bytes));
    }


    public static void main19(String[] args) {
        byte[] bytes = {97,98,99};
        String str = new String(bytes);
        System.out.println(str);
    }



    public static boolean isNumber(String str){
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if(ch < 48 || ch > 57){
                return false;
            }
        }
        return true;
    }

    public static void  main18(String[] args) {
        String str = "1a223";
        System.out.println(isNumber(str));

    }




    public static void main17(String[] args) {
        String str = "hello";
        char ch = str.charAt(1);
        System.out.println(ch);
    }


    public static void main16(String[] args) {
        String str = "hello";
        char[] chars = str.toCharArray();
        System.out.println(Arrays.toString(chars));
    }


    public static void main15(String[] args) {
        char[] chars = {'a','b','c','d'};
        String str = new String(chars);
        String str2 = new String(chars,1,3);
        System.out.println(str);
        System.out.println(str2);
    }
    public static void main14(String[] args) throws NoSuchFieldException, IllegalAccessException {
        String str = "Hello";
        Class cl = String.class;
        Field field = cl.getDeclaredField("value");
        field.setAccessible(true);
        char[] value = (char[]) field.get(str);
        value[0] = 'h';
        System.out.println(str);
    }


    public static void main13(String[] args) {
        String str = "Hello";
        str = "h" + str.substring(1);
        System.out.println("str");//没有改变真正的str
    }

    public static void main12(String[] args) {
        String str2 = "11";
        String str1 = new String("1") + new String("1");
        str1.intern();
        System.out.println(str1 == str2);//false
    }

    public static void main11(String[] args) {
        String str1 = new String("1") + new String("1");
        str1.intern();
        String str2 = "11";
        System.out.println(str1 == str2);//true
    }
    public static void main10(String[] args) {
        String str1 = new String("hello");
        //str1 = str1.intern();//true
        str1.intern();//false
        String str2 = "hello";
        System.out.println(str1 == str2);
    }



    public static void main9(String[] args) {
         String str1 = "hello";
         //String str1 = null;
        System.out.println(str1.equals("hello"));//可以发现空指针异常
        System.out.println("hello".equals(str1));//不会发生异常
    }

    public static void main8(String[] args) {
        String str1 = "hello";
        String str4 = "he"+ "llo";//常量拼接编译时拼接，已经改为hello常量池中有
        System.out.println(str1 == str4);//true
    }

    public static void main7(String[] args) {
        String str1 = "hello";
        String str2 = "he";
        String str3 = "llo";
        String str4 = str3 + str2;//变量拼接运行时拼接
        System.out.println(str1 == str4);//false
    }

    public static void main6(String[] args) {
        String str1 = "hello";
        String str2 = new String("he" ) + new String("llo");
        System.out.println(str1 == str2);//false
    }
    public static void main5(String[] args) {
        String str1 = "hello";
        String str2 = "hello";
        System.out.println(str1 == str2);//true
    }

    public static void main4(String[] args) {
        String str1 = "hello";
        String str2 = "he" + new String("llo");
        System.out.println(str1 == str2);//false
    }

    public  static  void func(String s,char[] ch){
        s = "bit";
        ch[0] = 'p';

    }

    public static void main3(String[] args) {
        String str1 = "hello";
        char[] chars = {'b','i','t'};
        func(str1,chars);
        System.out.println(str1);
        System.out.println(Arrays.toString(chars));
    }

    public static void main2(String[] args) {
        String str1 = "hello";
        String str2 = new String("hello");
        System.out.println(str1 == str2); // false
        System.out.println(str1.equals(str2));
    }




    public static void main1(String[] args) {
        //直接写法
        String str = "ADDDEE";

        //构造方法
        String str2 = new String("hello");

        //字符转字符串
        char[] chs = {'a','b','c'};
        String str3 = new String(chs);


        System.out.println(str);
        System.out.println(str2);
        System.out.println(str3);

    }
}
